<template>
  <el-dialog
    :title="dialogType ==='dialog_add'? text_add : text_update"
    :close-on-click-modal="isDebug"
    :visible.sync="dialogVisible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  label-width="130px" label-position="right">
    <el-form-item label="时间" prop="time">
      <el-input v-model="dataForm.time" placeholder="时间"></el-input>
    </el-form-item>
    <el-form-item label="直播室id" prop="autoid">
      <el-input v-model="dataForm.autoid" placeholder="直播室id"></el-input>
    </el-form-item>
    <el-form-item label="前值" prop="prevalue">
      <el-input v-model="dataForm.prevalue" placeholder="前值"></el-input>
    </el-form-item>
    <el-form-item label="实值" prop="annvalue">
      <el-input v-model="dataForm.annvalue" placeholder="实值"></el-input>
    </el-form-item>
    <el-form-item label="用户的手机号" prop="day">
      <el-input v-model="dataForm.day" placeholder="用户的手机号"></el-input>
    </el-form-item>
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="重要信息提示" prop="importence">
      <el-input v-model="dataForm.importence" placeholder="重要信息提示"></el-input>
    </el-form-item>
    <el-form-item label="重要话题" prop="question">
      <el-input v-model="dataForm.question" placeholder="重要话题"></el-input>
    </el-form-item>
    <el-form-item label="用户的手机号" prop="imageurl">
      <el-input v-model="dataForm.imageurl" placeholder="用户的手机号"></el-input>
    </el-form-item>
    <el-form-item label="用户的手机号" prop="goodpers">
      <el-input v-model="dataForm.goodpers" placeholder="用户的手机号"></el-input>
    </el-form-item>
    <el-form-item label="用户的手机号" prop="badpers">
      <el-input v-model="dataForm.badpers" placeholder="用户的手机号"></el-input>
    </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="dataForm.createTime"
          type="datetime"
          value-format="timestamp"
          placeholder="选择时间">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">{{text_cancel}}</el-button>
      <el-button type="primary" @click="dialogType ==='dialog_add'?requestAdd():requestUpdate()">{{text_confirm}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        dialogVisible: false,
        dialogType: 'dialog_add', // 弹窗类型 1添加 2编辑,
        dataForm: {
          id: 0,
          time: '',
          autoid: '',
          prevalue: '',
          annvalue: '',
          day: '',
          name: '',
          importence: '',
          question: '',
          imageurl: '',
          goodpers: '',
          badpers: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          time: [{ validator: this.checkText, trigger: 'change' }],
          autoid: [{ validator: this.checkText, trigger: 'change' }],
          prevalue: [{ validator: this.checkText, trigger: 'change' }],
          annvalue: [{ validator: this.checkText, trigger: 'change' }],
          day: [{ validator: this.checkText, trigger: 'change' }],
          name: [{ validator: this.checkText, trigger: 'change' }],
          importence: [{ validator: this.checkText, trigger: 'change' }],
          question: [{ validator: this.checkText, trigger: 'change' }],
          imageurl: [{ validator: this.checkText, trigger: 'change' }],
          goodpers: [{ validator: this.checkText, trigger: 'change' }],
          badpers: [{ validator: this.checkText, trigger: 'change' }],
          createTime: [{ validator: this.checkText, trigger: 'change' }],
          updateTime: [{ validator: this.checkText, trigger: 'change' }]
        }
      }
    },
    props: {
      rowData: {
        type: Object,
        default: null
      }
    },
    methods: {
      initDialog (dialogType) {
        this.dialogVisible = true
        this.dialogType = dialogType
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (dialogType === 'dialog_update') {
            this.requestInfo()
          }
        })
      },
      /** 请求信息(表单回显) */
      requestInfo () {
        this.$httpRequest({
          url: `/api/importantmessage/info/${this.rowData.id}`,
          method: 'get'
        }).then((data) => {
          this.dataForm.time = data.time
          this.dataForm.autoid = data.autoid
          this.dataForm.prevalue = data.prevalue
          this.dataForm.annvalue = data.annvalue
          this.dataForm.day = data.day
          this.dataForm.name = data.name
          this.dataForm.importence = data.importence
          this.dataForm.question = data.question
          this.dataForm.imageurl = data.imageurl
          this.dataForm.goodpers = data.goodpers
          this.dataForm.badpers = data.badpers
          this.dataForm.createTime = data.createTime
          this.dataForm.updateTime = data.updateTime
        }, error => {
          this.showErrorToast(error.msg)
        })
      },
       /** 请求添加 */
      requestAdd () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$httpRequest({
              url: `/api/importantmessage/add`,
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'time': this.dataForm.time,
                'autoid': this.dataForm.autoid,
                'prevalue': this.dataForm.prevalue,
                'annvalue': this.dataForm.annvalue,
                'day': this.dataForm.day,
                'name': this.dataForm.name,
                'importence': this.dataForm.importence,
                'question': this.dataForm.question,
                'imageurl': this.dataForm.imageurl,
                'goodpers': this.dataForm.goodpers,
                'badpers': this.dataForm.badpers,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              }
            }).then(() => {
              this.dialogVisible = false
              this.showSuccessToast(this.toast_text_add_success, () => {
                this.$emit('refreshDataList')
              })
            }, error => {
              this.showErrorToast(error.msg)
            })
          }
        })
      },
      /** 请求更新 */
      requestUpdate () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$httpRequest({
              url: `/api/importantmessage/add`,
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'time': this.dataForm.time,
                'autoid': this.dataForm.autoid,
                'prevalue': this.dataForm.prevalue,
                'annvalue': this.dataForm.annvalue,
                'day': this.dataForm.day,
                'name': this.dataForm.name,
                'importence': this.dataForm.importence,
                'question': this.dataForm.question,
                'imageurl': this.dataForm.imageurl,
                'goodpers': this.dataForm.goodpers,
                'badpers': this.dataForm.badpers,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              }
            }).then(() => {
              this.dialogVisible = false
              this.showSuccessToast(this.toast_text_add_success, () => {
                this.$emit('refreshDataList')
              })
            }, error => {
              this.showErrorToast(error.msg)
            })
          }
        })
      }
    }
  }
</script>
